package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class XiGongDa {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "";
    public static final String REFERER = "http://nwpup.iyuecloud.com/";
    public static final String URL_PRE = " http://press.cau.edu.cn";


    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum =1;
        int totalPage = 500;
        int pageSize = 9;
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "http://nwpup.iyuecloud.com/apis/Book/GetBooksByCategory";
            Map<String, Object> parameter = MapUtil.newHashMap(3);
            parameter.put("page",i);
            parameter.put("limit", pageSize);
            parameter.put("categoryCode", ".6.");
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Referer", REFERER);
            String result = HttpUtil.doGet(url, headers, parameter);
            JSONObject jsonObject = JSONObject.parseObject(result);
            Integer errcode = jsonObject.getInteger("status");
            if (errcode != null && errcode == 200) {
                JSONArray listObject = jsonObject.getJSONArray("result");
                int size = listObject.size();
                List<CxgBook> listBook = new ArrayList<>();
                for (int k = 0; k < size; k++) {
                    JSONObject jsonTmp = listObject.getJSONObject(k);
                    CxgBook bookEntity = new CxgBook();
                    bookEntity.setPkId(jsonTmp.getString("guid"));
                    bookEntity.setIsSync(0);
                    bookEntity.setCreateTime(LocalDateTime.now());
                    bookEntity.setPressName("西北工业大学出版社");
                    bookEntity.setStatus(1);
                    //出版社Id不明
                    bookEntity.setPressId(201);
                    CxgBook tempEntity = getBookEntity(jsonTmp.getString("guid"),bookEntity);
                    listBook.add(tempEntity);
                }
                bookService.saveBatch(listBook);
                log.info("第" + i + "页数据保存成功");
                log.warn("--->" + i);
                }
            }
    }

    public CxgBook getBookEntity(String numId,CxgBook bookEntity) throws Exception {
        String url = "http://nwpup.iyuecloud.com/apis/Book/Detail?guid="+numId;
        String result = HttpUtil.doGet(url);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Integer errcode = jsonObject.getInteger("status");
        if (errcode != null && errcode == 200) {
            JSONObject bookObj = jsonObject.getJSONObject("result");
            bookEntity.setBookName(bookObj.getString("bookName"));
            bookEntity.setAuthor(bookObj.getString("author"));
            bookEntity.setBookIsbn(bookObj.getString("bookISBN").replace("-",""));
            bookEntity.setCover(bookObj.getString("coverUrl"));
            bookEntity.setPrice(bookObj.getBigDecimal("price"));
            JSONArray resArr = bookObj.getJSONArray("resources");
            if (resArr.size()>0) {
                JSONObject resObj = resArr.getJSONObject(0);
                bookEntity.setDigitalUri(resObj.getString("resourceUrl"));
                bookEntity.setBrief(bookObj.getString("contentSummary"));
            }
        }
        return bookEntity;
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
